package com.bsoft.gol.hcb.utils;

import ctd.controller.exception.ControllerException;
import ctd.dictionary.controller.DictionaryController;
import ctd.dictionary.DictionaryItem;
import ctd.schema.annotation.Dictionary;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuxj on 2016/7/31.
 */
public class Common {

    private static String getCaptureName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return name;
    }

    public static Object setValueToNull(Object bean) throws Exception {
        for (Field f : bean.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            if ("class java.lang.String".equals(f.getType().toString())) {
                Method getMethod = bean.getClass().getMethod("get" + getCaptureName(f.getName()));
                if (StringUtils.isEmpty(getMethod.invoke(bean).toString())) {
                    f.set(bean, null);
                }
            }
        }
        return bean;
    }

    /**
     * 获取bean的字典fields
     */
    private Map mapPutDicText(Object[] beans) throws Exception {
        Map dicMap = new HashMap();
        for (Object bean : beans) {
            for (Field f : bean.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                Dictionary dictionary = f.getAnnotation(Dictionary.class);
                if (dictionary != null) {
                    dicMap.put(f.getName(), dictionary.id());
                }
            }
        }
        return dicMap;
    }

    /**
     * 添加字典文本到Map实体
     *
     * @param data
     * @throws ControllerException
     */
    public static void mapPutKeyValue(Map<String, Object> data, Map<String, String> mapDic) throws ControllerException {
        ctd.dictionary.Dictionary dictionary;// = DictionaryController.instance().get("hcn.base.dictionary.HCN_yesAndNoType");
        for (String key : mapDic.keySet()) {
            String value = mapDic.get(key);
            if (data.containsKey(key)) {
                try {
                    dictionary = DictionaryController.instance().get(value);
                    putMap(data, dictionary, key);
                }
                catch (ControllerException ex)
                {
                    throw ex;
                }
            }
        }
    }

    /**
     * 添加字典文本到Map中
     *
     * @param deptMap
     * @param dictionary
     * @param keyName
     */
    private static void putMap(Map<String, Object> deptMap, ctd.dictionary.Dictionary dictionary, String keyName) throws ControllerException {
        try {
            DictionaryItem item;
            Object object = deptMap.get(keyName);
            String keyValue = object != null ? object.toString() : "";
            item = dictionary.getItem(keyValue);
            String text = "";
            if (item != null) {
                text = item.getText();
            }
            deptMap.put(keyName + "Text", text);
        } catch (Exception ex) {
            throw new ControllerException(0, ex.getMessage());
        }
    }
}
